package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lhh
 * @ClassName UserFollowController
 * @date 2022/9/24 11:44
 */

@RestController
@RequestMapping("userFollow")
public class UserFollowController {

    @Reference
    private UserFollowService userFollowService;

    @GetMapping("/auth/follow/{houseId}")
    private Result follow(@PathVariable Long houseId, HttpServletRequest request){

        UserInfo userInfo= (UserInfo) request.getSession().getAttribute("USER");
        UserFollow userFollow = new UserFollow();
        userFollow.setHouseId(houseId);
        userFollow.setUserId(userInfo.getId());
        userFollowService.save(userFollow);

        return Result.ok();
    }

    @GetMapping("auth/list/{pageNum}/{pageSize}")
    public Result getFollowList(@PathVariable Integer pageNum,
                                @PathVariable Integer pageSize,
                                HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Long userId = userInfo.getId();
        PageInfo<UserFollowVo> page=userFollowService.getFollowList(pageNum,pageSize,userId);
        return Result.ok(page);
    }

    @GetMapping("auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable Long id){
        userFollowService.remove(id);
        return  Result.ok();
    }
}
